package com.chris.homecare.screens
{
	import com.chris.homecare.components.BreadcrumbComponenet;
	import com.chris.homecare.components.BulletHeadingComp;
	import com.chris.homecare.components.HeaderComponent;
	import com.chris.homecare.data.FetchRemoteData;
	import com.chris.homecare.data.Job;
	import com.chris.homecare.utils.CommonUtils;
	import com.chris.homecare.utils.ScreenNames;
	
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.controls.Screen;
	import feathers.controls.ScrollContainer;
	import feathers.controls.ScrollText;
	import feathers.controls.text.TextFieldTextRenderer;
	import feathers.core.ITextRenderer;
	import feathers.layout.VerticalLayout;
	
	import starling.display.Quad;
	import starling.events.Event;
	
	[Event(name="triggerApplyJobScreen",type="starling.events.Event")]
	
	public class JobScreen extends Screen
	{
		public static const TRIGGER_APPLY_JOB_SCREEN:String = "triggerApplyJobScreen";
		
		private var contentLayoutGroup:LayoutGroup;
		private var headerComp:HeaderComponent;
		private var breadcrumbComp:BreadcrumbComponenet;
		private var applyBtn:Button;
		private var scrollText:ScrollText;
		private var bulletHeadingComp:BulletHeadingComp;
		private var scrollContainer:ScrollContainer;
		private var contentLabel:Label;
		public function JobScreen()
		{
			super();
		}
		
		override protected function initialize():void
		{
			CommonUtils.previousScreen = ScreenNames.JOBS_LIST_SCREEN;
			
			this.layout = new VerticalLayout();
			
			var jobData:Job = FetchRemoteData.choosenJobData;
			
			contentLayoutGroup = new LayoutGroup();
			var contentLayout:VerticalLayout = new VerticalLayout();
			contentLayout.horizontalAlign = VerticalLayout.HORIZONTAL_ALIGN_CENTER;
			contentLayoutGroup.layout = contentLayout;
			addChild(contentLayoutGroup);
			
			headerComp = new HeaderComponent();
			headerComp.addEventListener(Event.COMPLETE, onMenuBtnTrigger);
			contentLayoutGroup.addChild(headerComp);
			
			breadcrumbComp = new BreadcrumbComponenet("Jobs List", "Job");
			contentLayoutGroup.addChild(breadcrumbComp);
			
			bulletHeadingComp = new BulletHeadingComp(jobData.jobHeading);
			contentLayoutGroup.addChild(bulletHeadingComp);
			
			var gapingQuad1:Quad = new Quad(10,20*CommonUtils.scaleValue,0xFFFFFF);
			contentLayoutGroup.addChild(gapingQuad1);
			
			scrollContainer = new ScrollContainer();
			var containerLayout:VerticalLayout = new VerticalLayout();
			containerLayout.horizontalAlign = VerticalLayout.HORIZONTAL_ALIGN_CENTER;
			scrollContainer.layout = containerLayout;
			addChild(scrollContainer);
			
			contentLabel = new Label();
			contentLabel.textRendererFactory = function():ITextRenderer
			{
				var textRenderer:TextFieldTextRenderer = new TextFieldTextRenderer();
				textRenderer.textFormat = new TextFormat("Helvetica", 26*CommonUtils.scaleValue, 0x000000, null, null, null, null, null, TextFormatAlign.JUSTIFY);
				textRenderer.isHTML = true;
				return textRenderer;
			}
			contentLabel.textRendererProperties.wordWrap = true;
			contentLabel.text = jobData.jobDescription;
			scrollContainer.addChild(contentLabel);
			
			var gapingQuad2:Quad = new Quad(10,30*CommonUtils.scaleValue,0xFFFFFF);
			scrollContainer.addChild(gapingQuad2);
			
			applyBtn = new Button();
			applyBtn.label = "Apply Now";
			applyBtn.nameList.add("performance-btn");
			applyBtn.addEventListener(Event.TRIGGERED, onApplyBtnTrigger);
			scrollContainer.addChild(applyBtn);
			
			super.initialize();
		}
		
		override protected function draw():void
		{
			var stageWidth:int = stage.stageWidth;
			var stageHeight:int = stage.stageHeight;
			
			scrollContainer.height = stageHeight - scrollContainer.y - 20*CommonUtils.scaleValue;
			scrollContainer.width = stageWidth;
			contentLabel.width = stageWidth - 40*CommonUtils.scaleValue;
			super.draw();
		}
		
		private function onMenuBtnTrigger(event:Event):void
		{
			this.dispatchEventWith(Event.COMPLETE);
		}
		
		private function onApplyBtnTrigger(event:Event):void
		{
			this.dispatchEventWith(TRIGGER_APPLY_JOB_SCREEN, false, FetchRemoteData.choosenJobData);
		}
	}
}